package com.yao.spider.zhihu;

import com.yao.spider.core.httpclient.AbstractHttpClient;
import com.yao.spider.core.httpclient.IHttpClient;
import com.yao.spider.core.util.Config;
import com.yao.spider.core.util.Constants;
import com.yao.spider.core.util.SimpleThreadPoolExecutor;
import com.yao.spider.core.util.ThreadPoolMonitor;
import com.yao.spider.proxy.ProxyHttpClient;
import com.yao.spider.zhihu.task.GenernalPageTask;
import com.yao.spider.proxy.ProxyPool;
import com.yao.spider.zhihu.task.DetailListPageTask;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 单耀 on 2017/12/11.
 *
 */
public class ZhiHuHttpClient extends AbstractHttpClient implements IHttpClient{
    private static Logger logger = LoggerFactory.getLogger(ZhiHuHttpClient.class);
    private volatile static ZhiHuHttpClient instance;
    public volatile static boolean isStop = false;

    //统计用户数量
    public static AtomicInteger parseUserCount = new AtomicInteger(0);

    //详情列表页下载线程池
    private ThreadPoolExecutor detailListPageThreadPool;
    //详情页下载线程池
    private ThreadPoolExecutor detailPageThreadPool;

    private static String authorization;
    public ZhiHuHttpClient() {
        initHttpClient();
        initThreadPool();
    }

    public static String getAuthorization() {
        return authorization;
    }

    public void initHttpClient() {
    }

    public void startSpiderZhiHu(String url) {

    }

    public void startSpiderZhiHu() {
        authorization = initAuthorization();

        String startToken = Config.startUserToken;
        String startUrl = String.format(Constants.USER_FOLLOWEES_URL, startToken, 0);
        HttpGet request = new HttpGet(startUrl);
        request.setHeader("authorization","oauth " + ZhiHuHttpClient.getAuthorization());
        detailPageThreadPool.execute(new DetailListPageTask(request,Config.isProxy));
        manageHttpClient();
    }


    /**
     * 初始化 authorization
     * @return
     */
    private String initAuthorization() {
        logger.info("初始化authorization中...");
        String content = null;
        GenernalPageTask genernalPageTask = new GenernalPageTask(Config.startURL,true);
        genernalPageTask.run();
        content = genernalPageTask.getPage().getHtml();

        //获取oauth
        Pattern pattern = Pattern.compile("https://static\\.zhihu\\.com/heifetz/main\\.app\\.([0-9]|[a-z])*\\.js");
        Matcher matcher = pattern.matcher(content);
        String jsSrc = null;
        if (matcher.find()) {
            jsSrc = matcher.group(0);
        } else {
            throw new RuntimeException("not find javascript url");
        }
        String jsContent = null;
        GenernalPageTask jsPageTask = new GenernalPageTask(jsSrc, true);
        jsPageTask.run();
        jsContent = jsPageTask.getPage().getHtml();

        pattern = Pattern.compile("oauth\\\"\\),h=\\\"(([0-9]|[a-z])*)\"");
        matcher = pattern.matcher(jsContent);
        if (matcher.find()) {
            String authorization = matcher.group(1);
            logger.info("初始化authorization完成");
            return authorization;
        }
        throw new RuntimeException("not get authorization");

    }

    public static ZhiHuHttpClient getInstance() {
        if (instance == null) {
            synchronized (ZhiHuHttpClient.class) {
                if (instance == null) {
                    instance = new ZhiHuHttpClient();
                }
            }
        }
        return instance;
    }

    public ThreadPoolExecutor getDetailListPageThreadPool() {
        return detailListPageThreadPool;
    }

    /**
     * 管理知乎客户端
     * 关闭整个爬虫
     */

    public void manageHttpClient() {
        //TODO
        while(true) {
            //下载网页数
            long downloadPageCount = detailListPageThreadPool.getTaskCount();
            if (downloadPageCount >= Config.downloadPageCount && !detailListPageThreadPool.isShutdown()) {
                isStop = true;
                ThreadPoolMonitor.isMonitor = true;
                detailListPageThreadPool.shutdown();
            }
            if (detailListPageThreadPool.isTerminated()) {
                //关闭测试代理线程池
                ProxyHttpClient.getInstance().getProxyTestThreadExector().shutdown();
                //关闭下载代理线程池
                ProxyHttpClient.getInstance().getProxyTestThreadExector().shutdown();

                break;
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //初始化线程池
    private void initThreadPool () {
        detailListPageThreadPool = new SimpleThreadPoolExecutor(Constants.downloadTreadSize,
                Constants.downloadTreadSize,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(),
                "detailListPageThreadPool");

        detailPageThreadPool = new SimpleThreadPoolExecutor(Constants.downloadTreadSize,
                Constants.downloadTreadSize,
                0L,TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(),
                "detailPateThreadPool"
                );

        new ThreadPoolMonitor(detailListPageThreadPool,"DetailListPageThreadPool");
    }
}
